package com.zeny.coding.controller.admin.business;


import com.zeny.coding.constants.CourseConstants;
import com.zeny.coding.dto.PageDto;
import com.zeny.coding.dto.Response;
import com.zeny.coding.dto.TeacherDto;
import com.zeny.coding.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.xml.bind.ValidationException;
import java.util.List;

/**
 * <h2>讲师</h2>
 *
 * @Author Zeny
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/business/admin/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @PostMapping("/list")
    public Response list(@RequestBody PageDto<TeacherDto> pageDto) {
        pageDto.check();
        teacherService.list(pageDto);
        return new Response().success().data(pageDto).message(CourseConstants.QUERY_SUCCESS);
    }

    @PostMapping("/save")
    public Response save(@RequestBody TeacherDto teacherDto) throws ValidationException {
        teacherDto.isValidForSave();
        this.teacherService.save(teacherDto);
        return new Response().success().message(CourseConstants.SAVE_SUCCESS);
    }

    @PutMapping("/update")
    public Response update(@RequestBody TeacherDto teacherDto) throws ValidationException {
        teacherDto.isValidForUpdate();
        this.teacherService.update(teacherDto);
        return new Response().success().message(CourseConstants.UPDATE_SUCCESS);
    }

    @GetMapping("/findById/{id}")
    public Response findById(@PathVariable("id") String id) {
        if (StringUtils.isEmpty(id)) {
            return new Response().fail().message(CourseConstants.REQUEST_PARAMS_ERROR);
        }
        TeacherDto teacherDto = this.teacherService.findById(id);
        return new Response()
                .success()
                .data(teacherDto)
                .message(CourseConstants.QUERY_SUCCESS);
    }

    @DeleteMapping("/deleteById/{id}")
    public Response deleteById(@PathVariable("id") String id) {
        if (StringUtils.isEmpty(id)) {
            return new Response().fail().message(CourseConstants.REQUEST_PARAMS_ERROR);
        }
        this.teacherService.deleteById(id);
        return new Response().success().message(CourseConstants.DELETE_SUCCESS);
    }

    @GetMapping("/all")
    public Response all() {
        List<TeacherDto> teacherDtoList = this.teacherService.findAll();
        return new Response().success().data(teacherDtoList).message(CourseConstants.QUERY_SUCCESS);
    }
}
